package myky.com.doctorapp.presenter;

import java.util.List;

import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import myky.com.doctorapp.base.BaseEntity;
import myky.com.doctorapp.base.BasePresenterIm;
import myky.com.doctorapp.base.RxSubscribe;
import myky.com.doctorapp.contract.HealthPersonInfoContract;
import myky.com.doctorapp.entity.HealthRecordEntity;
import myky.com.doctorapp.model.HealthPersonInfoModel;
import myky.com.doctorapp.utils.ILog;

public class HealthPersonInfoPresenter extends BasePresenterIm<HealthPersonInfoContract.View> implements HealthPersonInfoContract.Presenter {

    HealthPersonInfoModel healthPersonInfoModel;

    public HealthPersonInfoPresenter() {
        healthPersonInfoModel = new HealthPersonInfoModel();
    }

    @Override
    public void getPersionInfo() {


        healthPersonInfoModel.healthContacts().doOnNext(perProfileBeanBaseEntity -> {


            HealthRecordEntity.PerProfileBean payLoad = perProfileBeanBaseEntity.payLoad;

            List<HealthRecordEntity.PerProfileBean.FirstContactsListsBean> firstContactsLists = payLoad.getFirstContactsLists();

            if (firstContactsLists != null) {
                mView.showHeathConteacts(firstContactsLists);
            }

        }).flatMap((Function<BaseEntity<HealthRecordEntity.PerProfileBean>,
                ObservableSource<BaseEntity<HealthRecordEntity>>>) perProfileBeanBaseEntity ->
                healthPersonInfoModel.getPersionInfo()).subscribe(new RxSubscribe<HealthRecordEntity>() {
            @Override
            protected void onSuccess(HealthRecordEntity healthRecordEntity) {
                ILog.i(Thread.currentThread().getName());
                mView.hideLoading();
                mView.showDataSuccess(healthRecordEntity.getPerProfile());


                String illnessNames = healthRecordEntity.getPerProfile().getIllnessNames();

                mView.showDisseaseList(illnessNames);
            }

            @Override
            protected void onFailed(int code, String msg) {
                mView.logCat(msg);
                mView.hideLoading();
            }

            @Override
            public void onSubscribe(Disposable d) {

                addSubscription(d);
                mView.showLoadingView("请稍后");

            }
        });

    }
}
